﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GPMS.DbTest
{
    using GPMS.Model;
    public class TestData
    {
        GPMSDbContainer gpms = new GPMSDbContainer();

        /// <summary>
        /// 添加教学系测试数据
        /// </summary>
        public void AddSpecialty()
        {
            List<Specialty> specialties = new List<Specialty>()
            {
                new Specialty(){ SpecialtyName="网络系",SpecialtyID= Guid.NewGuid()},
                new Specialty(){ SpecialtyName="软件系",SpecialtyID= Guid.NewGuid()},
                new Specialty(){ SpecialtyName="游戏系",SpecialtyID= Guid.NewGuid()},
                new Specialty(){ SpecialtyName="电子系",SpecialtyID= Guid.NewGuid()},
                new Specialty(){ SpecialtyName="外语系",SpecialtyID= Guid.NewGuid()},
                new Specialty(){ SpecialtyName="管理系",SpecialtyID= Guid.NewGuid()},
                new Specialty(){ SpecialtyName="国际经济与贸易系",SpecialtyID= Guid.NewGuid()},
                new Specialty(){ SpecialtyName="数码媒体系",SpecialtyID= Guid.NewGuid()},
                new Specialty(){ SpecialtyName="计算机系",SpecialtyID= Guid.NewGuid()},
            };
            specialties.ForEach(s => gpms.Specialties.AddObject(s));
            gpms.SaveChanges();
            Console.WriteLine("添加教学系测试数据完毕");
        }


        /// <summary>
        /// 添加用户测试数据
        /// </summary>
        public void AddUserInfo()
        {
            List<UserInfo> UserInfos = new List<UserInfo>()
            {
                new UserInfo(){ UserName="AA", RealName="A", Password="0", UserIdentity="111111", IsDeleted=false, IsLocked=false, 
                    CreateDate=DateTime.Now, Specialty=gpms.Specialties.FirstOrDefault(s=>s.SpecialtyName=="网络系"),
                    UserID=Guid.NewGuid()},
                new UserInfo(){ UserName="BB", RealName="B", Password="0", UserIdentity="222222", IsDeleted=false, IsLocked=false, 
                    CreateDate=DateTime.Now, Specialty=gpms.Specialties.FirstOrDefault(s=>s.SpecialtyName=="网络系"),
                    UserID=Guid.NewGuid()},
                new UserInfo(){ UserName="CC", RealName="C", Password="0", UserIdentity="333333", IsDeleted=false, IsLocked=false, 
                    CreateDate=DateTime.Now, Specialty=gpms.Specialties.FirstOrDefault(s=>s.SpecialtyName=="软件系"),
                    UserID=Guid.NewGuid()},
                new UserInfo(){ UserName="DD", RealName="D", Password="0", UserIdentity="444444", IsDeleted=false, IsLocked=false, 
                    CreateDate=DateTime.Now, Specialty=gpms.Specialties.FirstOrDefault(s=>s.SpecialtyName=="软件系"),
                    UserID=Guid.NewGuid()}
            };
            UserInfos.ForEach(u => gpms.UserInfos.AddObject(u));
            gpms.SaveChanges();
            Console.WriteLine("添加用户测试数据完毕");
        }

        /// <summary>
        /// 添加站内信息测试数据
        /// </summary>
        public void AddMessage()
        {
            List<Message> messages = new List<Message>
            {
                new Message(){ Context="Hello BB", Sender=gpms.UserInfos.FirstOrDefault(u=>u.UserName=="AA"),Title="hi",
                 Receiver=gpms.UserInfos.FirstOrDefault(u=>u.UserName=="BB"), CreateDate=DateTime.Now, IsDeletedReceiver=false,IsDeletedSender=false ,IsRead=false,MessageID=Guid.NewGuid()},
                 new Message(){ Context="Hello AA", Sender=gpms.UserInfos.FirstOrDefault(u=>u.UserName=="CC"),Title="hello",
                 Receiver=gpms.UserInfos.FirstOrDefault(u=>u.UserName=="AA"), CreateDate=DateTime.Now, IsDeletedReceiver=false,IsDeletedSender=false, IsRead=false,MessageID=Guid.NewGuid()}
            };
            messages.ForEach(m => gpms.Messages.AddObject(m));
            gpms.SaveChanges();
            Console.WriteLine("添加站内信息测试数据完毕");
        }

        /// <summary>
        /// 添加毕业设计题目测试数据
        /// </summary>
        public void AddGraduationProject()
        {
            List<GraduationProject> projects = new List<GraduationProject>()
            {
                new GraduationProject(){ GPName="图书馆里系统", Description="BMS", Type="设计", CreateDate=DateTime.Now,
                 Guider=gpms.UserInfos.FirstOrDefault(u=>  u.Specialty.SpecialtyName=="网络系"),GPID=Guid.NewGuid()},
                 new GraduationProject(){ GPName="超级精英系统", Description="BMS", Type="设计", CreateDate=DateTime.Now,
                 Guider=gpms.UserInfos.FirstOrDefault(u=> u.Specialty.SpecialtyName=="软件系"),GPID=Guid.NewGuid()}
            };

            projects.ForEach(p => gpms.GraduationProjects.AddObject(p));
            gpms.SaveChanges();
            Console.WriteLine("添加毕业设计题目测试数据完毕");
        }

        /// <summary>
        /// 添加毕业设计题目选择测试数据
        /// </summary>
        public void AddGPSelection()
        {
            List<GPSelection> gps = new List<GPSelection>()
            {
                new GPSelection(){ User=gpms.UserInfos.FirstOrDefault(u=>u.UserName=="BB"),
                 GraduationProject=gpms.GraduationProjects.FirstOrDefault(p=>p.GPName=="图书馆里系统"),
                SelectDate=DateTime.Now, IsConfirm=false,SelectionID=Guid.NewGuid()},
                new GPSelection(){ User=gpms.UserInfos.FirstOrDefault(u=>u.UserName=="BB"),
                 GraduationProject=gpms.GraduationProjects.FirstOrDefault(p=>p.GPName=="超级精英系统"),
                SelectDate=DateTime.Now, IsConfirm=false,SelectionID=Guid.NewGuid()},
                new GPSelection(){ User=gpms.UserInfos.FirstOrDefault(u=>u.UserName=="DD"),
                 GraduationProject=gpms.GraduationProjects.FirstOrDefault(p=>p.GPName=="超级精英系统"),
                SelectDate=DateTime.Now, IsConfirm=false,SelectionID=Guid.NewGuid()}
            };
            gps.ForEach(g => gpms.GPSelections.AddObject(g));
            gpms.SaveChanges();
            Console.WriteLine("添加毕业设计题目选择测试数据完毕");
        }

        ///// <summary>
        ///// 添加毕业设计计划测试数据
        ///// </summary>
        //public void AddProjectPlan()
        //{
        //    var project = gpms.GraduationProjects.FirstOrDefault(p => p.GPName == "超级精英系统");
        //    List<ProjectPlan> plans = new List<ProjectPlan>()
        //    {
        //        new ProjectPlan(){ Description="超级精英系统001", IsDone=false,GPID=project.GPID,
        //        BeginDate=new DateTime(2010,10,1),FinishDate=new DateTime(2010,10,20),PlanID=Guid.NewGuid()},
        //        new ProjectPlan(){ Description="超级精英系统002", IsDone=false,GPID=project.GPID,
        //        BeginDate=new DateTime(2010,10,25),FinishDate=new DateTime(2010,10,30),PlanID=Guid.NewGuid()},
        //        new ProjectPlan(){ Description="超级精英系统003", IsDone=false,GPID=project.GPID,
        //        BeginDate=new DateTime(2010,11,1),FinishDate=new DateTime(2010,11,10),PlanID=Guid.NewGuid()}
        //    };
        //    plans.ForEach(p => gpms.ProjectPlans.AddObject(p));
        //    //project = gpms.GraduationProjects.FirstOrDefault(p => p.GPName == "图书馆里系统");
        //    //plans = new List<ProjectPlan>()
        //    //{
        //    //    new ProjectPlan(){ Description="图书馆里系统001", IsDone=false,GPID=project.GPID,
        //    //    BeginDate=new DateTime(2010,10,1),FinishDate=new DateTime(2010,10,20),PlanID=Guid.NewGuid()},
        //    //    new ProjectPlan(){ Description="图书馆里系统002", IsDone=false,GPID=project.GPID,
        //    //    BeginDate=new DateTime(2010,10,25),FinishDate=new DateTime(2010,10,30),PlanID=Guid.NewGuid()},
        //    //    new ProjectPlan(){ Description="图书馆里系统003", IsDone=false,GPID=project.GPID,
        //    //    BeginDate=new DateTime(2010,11,1),FinishDate=new DateTime(2010,11,10),PlanID=Guid.NewGuid()}
        //    //};
        //    gpms.SaveChanges();
        //    Console.WriteLine("添加毕业设计计划测试数据完毕");
        //}

        /// <summary>
        /// 添加角色测试数据
        /// </summary>
        public void AddRole()
        {
            var uAA = (from u in gpms.UserInfos
                      where u.UserName == "AA"
                      select u).FirstOrDefault();
            var uBB = (from u in gpms.UserInfos
                       where u.UserName == "BB"
                       select u).FirstOrDefault();
            var uCD =  from u in gpms.UserInfos
                       where u.UserName == "CC" || u.UserName == "DD"
                       select u;
            List<Role> roles = new List<Role>()
            {
                new Role(){ RoleName="R for AA", IsDeleted=false, CreateDate=DateTime.Now,RoleID=Guid.NewGuid()},
                new Role(){ RoleName="R for BB", IsDeleted=false, CreateDate=DateTime.Now,RoleID=Guid.NewGuid()},
                new Role(){ RoleName="R for ALL", IsDeleted=false, CreateDate=DateTime.Now,RoleID=Guid.NewGuid()},
                new Role(){ RoleName="R for AA&BB", IsDeleted=false, CreateDate=DateTime.Now,RoleID=Guid.NewGuid()}
            };
            uAA.Roles.Add(roles[0]);
            uAA.Roles.Add(roles[2]);
            uAA.Roles.Add(roles[3]);

            uBB.Roles.Add(roles[1]);
            uBB.Roles.Add(roles[2]);
            uBB.Roles.Add(roles[3]);

            foreach (var u in uCD)
            {
                u.Roles.Add(roles[2]);
            }

            roles.ForEach(r => gpms.Roles.AddObject(r));
            gpms.SaveChanges();

            Console.WriteLine("添加角色测试数据完毕");
        }

        /// <summary>
        /// 添加ControllerInfo测试数据
        /// </summary>
        public void AddControllerInfo()
        {
            List<ControllerInfo> cs = new List<ControllerInfo>()
            {
                new ControllerInfo(){ ControllerName="Home", CreateDate=DateTime.Now, IsDeleted=false,ControllerID=Guid.NewGuid()},
                new ControllerInfo(){ ControllerName="Account", CreateDate=DateTime.Now, IsDeleted=false,ControllerID=Guid.NewGuid()},
                new ControllerInfo(){ ControllerName="Role", CreateDate=DateTime.Now, IsDeleted=false,ControllerID=Guid.NewGuid()},
                new ControllerInfo(){ ControllerName="Premission", CreateDate=DateTime.Now, IsDeleted=false,ControllerID=Guid.NewGuid()}
            };
            cs.ForEach(c => gpms.ControllerInfos.AddObject(c));
            gpms.SaveChanges();
            Console.WriteLine("添加ControllerInfo测试数据完毕");
        }

        /// <summary>
        /// 添加ActionInfo测试数据
        /// </summary>
        public void AddActionInfo()
        {
            var controllerInfo = (from c in gpms.ControllerInfos
                     where c.ControllerName == "Home"
                     select c).FirstOrDefault();
            List<ActionInfo> ash = new List<ActionInfo>()
            {
                new ActionInfo(){ ActionName="Index", CreateDate=DateTime.Now, IsDeleted=false, ControllerInfo=controllerInfo,ActionID=Guid.NewGuid()},
                new ActionInfo(){ ActionName="About", CreateDate=DateTime.Now, IsDeleted=false, ControllerInfo=controllerInfo,ActionID=Guid.NewGuid()}
            };
            ash.ForEach(a => gpms.ActionInfos.AddObject(a));

            controllerInfo = (from c in gpms.ControllerInfos
                          where c.ControllerName == "Premission"
                          select c).FirstOrDefault();
            ash = new List<ActionInfo>()
            {
                new ActionInfo(){ ActionName="Index", CreateDate=DateTime.Now, IsDeleted=false, ControllerInfo=controllerInfo},
                new ActionInfo(){ ActionName="TestActionInfo1", CreateDate=DateTime.Now, IsDeleted=false, ControllerInfo=controllerInfo,ActionID=Guid.NewGuid()},
                new ActionInfo(){ ActionName="TestActionInfo2", CreateDate=DateTime.Now, IsDeleted=false, ControllerInfo=controllerInfo,ActionID=Guid.NewGuid()},
                new ActionInfo(){ ActionName="TestActionInfo3", CreateDate=DateTime.Now, IsDeleted=false, ControllerInfo=controllerInfo,ActionID=Guid.NewGuid()},
                new ActionInfo(){ ActionName="TestActionInfo4", CreateDate=DateTime.Now, IsDeleted=false, ControllerInfo=controllerInfo,ActionID=Guid.NewGuid()},
                new ActionInfo(){ ActionName="TestActionInfo5", CreateDate=DateTime.Now, IsDeleted=false, ControllerInfo=controllerInfo,ActionID=Guid.NewGuid()}
            };
            ash.ForEach(a => gpms.ActionInfos.AddObject(a));

            gpms.SaveChanges();
            AddRoleUser(ash);
            Console.WriteLine("添加ActionInfo测试数据完毕");
        }

        /// <summary>
        /// 添加RoleUser测试数据
        /// </summary>
        /// <param name="acs"></param>
        private void AddRoleUser(List<ActionInfo> acs)
        {
            var rAA = (from u in gpms.Roles
                       where u.RoleName == "R for AA"
                       select u).FirstOrDefault();
            var rBB = (from u in gpms.Roles
                       where u.RoleName ==  "R for BB"
                       select u).FirstOrDefault();
            var rAB = (from u in gpms.Roles
                       where u.RoleName ==  "R for AA&BB"
                       select u).FirstOrDefault();

            rAA.ActionInfos.Add(acs[1]);
            rAA.ActionInfos.Add(acs[2]);
            rBB.ActionInfos.Add(acs[4]);
            rAB.ActionInfos.Add(acs[2]);
            rAB.ActionInfos.Add(acs[3]);
            gpms.SaveChanges();
            Console.WriteLine("添加RoleUser测试数据完毕");
        }
    }  
}
